package com.tsd.jxc.buy.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.common.controller.BaseController;
import com.tsd.core.annotation.DataFieldAuth;
import com.tsd.core.constants.Constants;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.JxcConstants;
import com.tsd.jxc.buy.entity.JxcBuyOrderDetail;
import com.tsd.jxc.buy.entity.JxcBuyOrderDetailExt;
import com.tsd.jxc.buy.service.JxcBuyOrderDetailService;
import com.tsd.jxc.req.JxcBuyReturnBillDetailReqParamsVo;
import com.tsd.jxc.setting.service.JxcSettingService;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.service.SystemSettingService;
import com.tsd.system.vo.GlobalSetting;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcBuyOrderDetail采购订单-明细 控制层
 *
 * @author Hillpool
 * @date 2022/07/29 15:00:45
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "进销存-采购订单-明细", description = "采购订单-明细的API描述")
public class JxcBuyOrderDetailController extends BaseController {

    @Resource
    private JxcBuyOrderDetailService jxcBuyOrderDetailService;
    @Resource
    private SystemSettingService systemSettingService;
    @Resource
    private JxcSettingService jxcSettingService;

    @ApiOperation(value = "根据id删除采购订单-明细", notes = "根据id删除采购订单-明细")
    @ApiImplicitParams({@ApiImplicitParam(name = "sids", value = "采购订单-明细sids", required = true, dataType = "string", paramType = "form")})
    @ResponseBody
    @RequestMapping(value = "/deleteJxcBuyOrderDetails.action", method = RequestMethod.POST)
    public HttpResult<String> deleteJxcBuyOrderDetails() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get("sids");
            super.checkEmpty(sids, "请提供参数sids");
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            jxcBuyOrderDetailService.deleteJxcBuyOrderDetails(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "分页查询采购订单-明细", notes = "分页查询采购订单-明细")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"), @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"), @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"), @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"), @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")})
    @ResponseBody
    @RequestMapping(value = "/findJxcBuyOrderDetails.action", method = RequestMethod.POST)
    public HttpResult<Object> findJxcBuyOrderDetails() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = new PageBean(map);
            List<JxcBuyOrderDetailExt> list = jxcBuyOrderDetailService.findJxcBuyOrderDetails(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    public HttpResult<Object> findJxcBuyOrderDetailList() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            if (!map.containsKey("group_sid")) {
                params.put("group_sid", opUser.getGroup_sid());
            }
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyIntegerValue(map, params, "state", "state", false, null);
            HlpUtils.copyIntegerValue(map, params, "order_type", "order_type", false, null);
            HlpUtils.copyIntegerValue(map, params, "audit_state", "audit_state", false, null);
            HlpUtils.copyStringValue(map, params, "sale_order_sid", "sale_order_sid", false, false);
            HlpUtils.copyStringValue(map, params, "group_sid", "group_sid", false, false);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            HlpUtils.copyDateRangeValue(map, params, "order_date", false);
            PageBean page = new PageBean(map);
            List<JxcBuyOrderDetailExt> list = jxcBuyOrderDetailService.findJxcBuyOrderDetails4Select(params, page);
            if (!HlpUtils.isEmptyList(list)) {
                GlobalSetting setting = systemSettingService.getGlobalSetting();
                ListUtil.forEach(list, t -> setting.procDisplay(t, GlobalSetting.TYPE_SUPPLIER));
            }
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "采购退货单选单", notes = "采购退货单选单")
    @ResponseBody
    @RequestMapping(value = "/queryJxcBuyOrderDetailList4Return.action", method = RequestMethod.POST)
    public HttpResult<Object> queryJxcBuyOrderDetailList4Return() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "supplier_sid", "supplier_sid", true, false);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            HlpUtils.copyListValue(map, params, "selected_list", "selected_list", false, String.class);
            PageBean page = new PageBean(map);
            if (map.containsKey(Constants.KEY_NO_PAGE)) {
                page = null;
            }
            List<JxcBuyOrderDetailExt> list = jxcBuyOrderDetailService.queryJxcBuyOrderDetailList4Return(params, page);
            JxcSettingVo settingVo = jxcSettingService.getJxcSettingVo(DataSourceContextHolder.getDataSourceType());
            if (settingVo.isClothing()) {
                hr.setData(JxcBuyReturnBillDetailReqParamsVo.transfer(list, settingVo));
            } else {
                hr.setData(list);
                hr.setPage(page);
            }
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
